
import 'dart:io';

import 'package:baselib/utils/gallery_camera_pick_util.dart';
import 'package:baselib/utils/keyboard_util.dart';
import 'package:baselib/utils/load_util.dart';
import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/navigator_util.dart';
import 'package:baselib/utils/toast_util.dart';
import 'package:flutter/src/widgets/editable_text.dart';
import 'package:framelib/base/base_view_model.dart';
import 'package:framelib/config/api_config.dart';
import 'package:framelib/config/route_config.dart';
import 'package:framelib/net/baseentity.dart';
import 'package:framelib/utils/drawable_util.dart';
import 'package:framelib/utils/nim_sdk_util.dart';
import 'package:framelib/utils/oss_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:framelib/widgets/page_widget.dart';
import 'package:get/state_manager.dart';
import 'package:get/get.dart';
import 'package:module_business/module_comm/model/interest_tag_entity.dart';
import 'package:module_business/module_comm/model/user_params_model.dart';
import 'package:module_business/module_comm/service/module_comm_service.dart';
import 'package:module_business/module_index/model/index_room_item_entity.dart';
import 'package:module_business/module_index/model/voice_person_entity.dart';
import 'package:module_business/module_index/service/module_index_service.dart';
import 'package:nim_core/nim_core.dart';

import 'index_view_model.dart';
import 'my_room_view_model.dart';

class RoomReleaseNotificationViewModel extends BaseViewModel<ModuleIndexService>{

  ModuleIndexService moduleIndexService;
  IndexRoomItemEntity roomInfo;
  NIMChatroomInfo roomYxInfo;
  String announcement = '';
  VoicePersonEntity voicePersonEntity = VoicePersonEntity();

  @override
  ModuleIndexService createService() =>moduleIndexService = ModuleIndexService();
  @override
  void onInit() {
    super.onInit();
    Map<String,dynamic> arguments = Get.arguments??{};
    roomInfo =  arguments['roomInfo'];
    roomYxInfo =  arguments['roomYxInfo'];
    announcement = roomYxInfo?.announcement??'';
    LogUtil.d('announcement-->$announcement');
    //查询此用户和此房间的关系
    queryUserRoomRelationType();
  }
  @override
  void onClose() {
    super.onClose();
  }

  save() async{
    if((announcement??'').isEmpty){
      ToastUtil.toast(StringUtil.instance.Set_the_room_notification);
      return;
    }
    NIMResult<void> updateResult = await NIMSDKUtil.instance
        .updateChatroomInfo(roomId: roomYxInfo?.roomId,request: NIMChatroomUpdateRequest(
      announcement: announcement,
    ));
    LogUtil.d('NIMSDKUtil,updateResult isSuccess =${updateResult.isSuccess}');
    if(updateResult.isSuccess){
      ToastUtil.toast(StringUtil.instance.Update_success);
      NavigatorUtil.goBack();
    }else{
      LogUtil.d('NIMSDKUtil,getMembers membersResult =${updateResult?.toString()}');
      ToastUtil.toast(StringUtil.instance.Update_fail);
    }
  }
  //查询此用户和此房间的关系
  void queryUserRoomRelationType() async{
    Map<String,dynamic> upData={
      "roomId": roomInfo?.id,
      "userId": UserParamsModel.instance.userId
    };
    BaseEntity baseEntity = await moduleIndexService.
    api_voicechat_room_queryUserRoomRelationType(parmsMap: upData);
    if(baseEntity?.code == ApiConfig.SUCCESS_CODE){
      voicePersonEntity = baseEntity?.result??VoicePersonEntity();
      update();
    }
  }
  //关系类型:2:管理员,3:主播,4:黑名单,6:禁言
  bool isHasPermission(){
    return (voicePersonEntity?.relationType == 1)||(voicePersonEntity?.relationType == 2);
  }
  notificationChange(value) {
    announcement = value;
  }
}
